package com.itheima.test;

/**
 * @Author:gy
 * @Date: 2025/09/04 8:50
 * @Description:
 */
public class RedPacksRunable implements Runnable{
    private String name;

    public RedPacksRunable(String name) {
        this.name = name;
    }

    //锁  红包个数  共享数据
    private static int redPacketCount = 3;
    private static  Object lock = new Object();
    private static final int REDPACKETS = 20;

    @Override
    public void run() {
        synchronized (lock) {
            //加锁  多线程操作了共享数据
            if(redPacketCount > 0){
                System.out.println("恭喜"+name+"您成功抢到一个"+REDPACKETS+"元的红包");
                redPacketCount--;
            }else{
                System.out.println(name+"抱歉,红包已经抢完了");
            }
        }
    }
}
